<?php
/**
* @file Eachfallow.php
* @synopsis  获得好友
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-05-14 17:46:55
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Eachfallow extends CI_Model
{
	function __construct()
	{
		$this->load->model('specialtym');
		$this->load->model('userm');
		$this->load->model('fellow_follow_model');
	}

	public function specifial()
	{
		$ret = array();
		$ret = $this->cache->get('Eachfallowspecifial');
		if($ret)
		{
			return $ret;
		}
		$s = $this->specialtym->get_specialt_list();
		$ns = [];
		foreach($s AS $sv)
		{
			$ns[$sv['specialty_code']] = $sv['specialty_name'];
		}
		$t = $this->specialtym->get_thread_list();
		$nt = [];
		$nt['-1'] = "医学生";
		$nt['-2'] = "实习医生";
		foreach($t AS $tv)
		{
			$nt[$tv['id']] = $tv['thread_name'];
		}
		$ret = ['s' => $ns,'t' => $nt];
		$this->cache->save('Eachfallowspecifial',$ret,86400);
		return $ret;
	}

	public function myfallow($user_id)
	{
		$query = $this->db->select('following')->from('ml_member_relation')->where('followers', $user_id)->get()->result_array();
		$data = array();
		foreach($query AS $value)
		{
			$data[] = $value['following'];
		}
		return $data;
	}

	public function fallowmy($user_id)
	{
		$query = $this->db->select('followers')->from('ml_member_relation')->where('following',$user_id)->get()->result_array();
		$data = array();
		foreach($query AS $value)
		{
			$data[] = $value['followers'];
		}
		return $data;
	}

	public function friendlist($user_id)
	{
		$mf = $this->myfallow((string)$user_id);
		$fm = $this->fallowmy((string)$user_id);
		return array_intersect($mf,$fm);
	}

	public function myFriendData($user_id)
	{
		$user_status = $this->fellow_follow_model->getPreviousTS( $user_id, KEY_PRE_TS_FRIENDS );
		# 上次查看好友列表时间
		$pre_ts = 0;
		if (isset($user_status[KEY_PRE_TS_FRIENDS]) && !empty($user_status[KEY_PRE_TS_FRIENDS])) {
			$pre_ts = $user_status[KEY_PRE_TS_FRIENDS];
		}
		# 新加的好友数目
		$num_new = 0;

		$list = $this->friendListAndTime($user_id);
		$data = array();
		$specifial = $this->specifial();
		foreach($list AS $uid => $itm)
		{
            if($uid == $this->uid)
            {
                continue;
            }
			//$uinfo = $this->db->select()->where(['id' => (string)$uid ])->get('ml_fellow')->result_array();
			$uinfo = $this->userm->get_one_user_4_search($uid,$this->uid);
			$ndata = [];
			$ndata['followers'] = $uid;
			$ndata['username'] = $uinfo['username'];
			$ndata['specialty'] = $uinfo['thread'] ? $specifial['t'][$uinfo['thread']] : $specifial['s'][$uinfo['specialty']];
			$ndata['addtime'] = '';
			$ndata['head_ico'] = $uinfo['head_ico'];
			$ndata['following_status'] = "3";
            $ndata['is_expert'] = $uinfo['is_expert'];
            $ndata['practice_hospital'] = $uinfo['practice_hospital'];
            $ndata['post_title'] = $uinfo['post_title'];
            $ndata['expert_info'] = $uinfo['expert_info'];
            $ndata['verified_status'] = $uinfo['verified_status'];
            $ndata['admin_level'] = $uinfo['admin_level'];
            if($ndata['is_expert'] == 1)
            {
                $ndata['truename'] = $uinfo['truename'];
            }
			# 是否是新好友
			$ndata['is_new'] = 0;
			if ($pre_ts && intval($pre_ts) <= intval($itm["atime"])) {
				// 上次查看列表之后确定好友关系的
				$ndata["is_new"] = 1;
				$num_new += 1;
			}
			# 是好友关系，与关注粉丝等接口作区分
			$ndata["is_my_friend"] = 1;
			$data[] = $ndata;
		}

		$result = ["follow_list"=>$data, "count"=>count($data), "num_new"=>$num_new];

		# 记录这次查看事件
		$this->fellow_follow_model->updateMyStatus( $user_id, KEY_PRE_TS_FRIENDS );

		return $result;
	}


	// 获取关注列表数据
	public function focusList( $user_id ) {
		$query = $this->db->select('following', 'addtime')->from('ml_member_relation')->where('followers', $user_id)->get()->result_array();
		$result = array();
		foreach ($query as $itm) {
			$result[$itm["following"]] = array("user_id" => $itm["following"], "atime" => intval(substr($itm["addtime"], 0, 10)));
		}
		return $result;
	}

	// 获取粉丝列表数据
	public function fansList( $user_id ) {
		$query = $this->db->select('followers', 'addtime')->from('ml_member_relation')->where('following' , $user_id)->get()->result_array();
		$result = array();

		foreach ($query as $itm) {
			$result[$itm["followers"]] = array("user_id" => $itm["followers"], "atime" => intval(substr($itm["addtime"], 0, 10)));
		}

		return $result;
	}

	// 获取用户好友及生成好友关系的时间
	public function friendListAndTime( $user_id ) {
		# 关注的
		$focus_list = $this->focusList( $user_id );
//		var_dump($focus_list);
		$fans_list = $this->fansList( $user_id );
//		var_dump($fans_list);

		$count_focus = count($focus_list);
		$count_fans = count($fans_list);

		$result = array();
		$small = $fans_list;
		$large = $focus_list;
		if ($count_focus < $count_fans) {
			$small = $focus_list;
			$large = $fans_list;
		}
		foreach ($small as $uid => $itm) {
			if (isset($large[$uid])) {
				$ts = ($small[$uid]["atime"]>$large[$uid]["atime"]) ? $small[$uid]["atime"] : $large[$uid]["atime"];
				$result[$uid] = array("user_id"=>$uid, "atime"=>$ts);
			}
		}

		# [123 =>[123 , 1234567890], ...]
		return $result;
	}
}
